using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SkeletonBattleState : EnemyState
{
    private Transform player;
    private Enemy_Skeleton enemy;
    private float moveDir = 1f;
    public SkeletonBattleState(Enemy _enemyBase, EnemyStateMachine _enemyStateMachine, Enemy_Skeleton _enemy,string _animBoolName) : base(_enemyBase, _enemyStateMachine, _animBoolName)
    {
        enemy = _enemy;
    }

    public override void Enter()
    {
        base.Enter();
        player = GameObject.Find("Player").transform;
        

    }

    public override void Exit()
    {
        base.Exit();
        
    }

    public override void Update()
    {
        base.Update();
        if(enemy.isPlayerDetected())
        {
            stateTimer = enemy.battleTime;
            if (enemy.isPlayerDetected().distance <= enemy.attackDistance)
            {
            if(canAttack())
            {
                StateMachine.changeState(enemy.AttackState);      

            }

            }
        }
        else
        {
            if (stateTimer < 0 && Vector2.Distance(player.transform.position, enemy.transform.position) > 5f) 
            {
                StateMachine.changeState(enemy.IdleState);
            }

        }

        if(player.position.x > enemy.transform.position.x)
        {
            moveDir = 1f;
        }else if(player.position.x < enemy.transform.position.x)
        {
            moveDir = -1f;        
        }
        enemy.setVelocity(enemy.moveSpeed * moveDir * 1.5f, rb.velocity.y);
    }
    public bool canAttack()
    {
        if(Time.time >= enemy.lastAttackTime + enemy.attackCoolDown)
        {
            enemy.lastAttackTime = Time.time;
            return true;
        }
        return false;
    }
}
